package com.delayqueue.master.common;

import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashSet;
import java.util.Set;

/**
 * ip白名单
 */
@Configuration
@ConfigurationProperties(prefix = "iptable")
public class IpTable {

    @Getter
    private Set<String> whiteList = new HashSet<>();

    public boolean isInWhiteList(String ip) {
        return whiteList.contains(ip);
    }

    public boolean isEnabled() {
        return !whiteList.isEmpty();
    }


    public void setWhiteList(String str) {
        if (str != null && !str.equals("")) {
            while (str.contains("  ")) {
                str = str.replace("  ", " ");
            }
            String[] strs = str.split(" ");
            for (String s : strs) {
                whiteList.add(s);
            }
        }
    }

}
